package game.Weapons;

import java.io.IOException;
import java.util.*;

import game.Scene;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class LoadWeapons {
    protected static LoadWeapons instance;
    private static PropertiesConfiguration configFile;
    private static ArrayList <Weapon_info> weapon_list;
    private static ArrayList <Weapon_info> counter_w_list;
    private static ArrayList <Weapon_info> terrorist_w_list;

    /**
     * Constructor of LoadWeapons:
     * Load all weapons of "config/weapons_info.cfg"
     * @throws ConfigurationException
     */
    public LoadWeapons() throws ConfigurationException, IOException, LineUnavailableException, UnsupportedAudioFileException {
        configFile = new PropertiesConfiguration();
        weapon_list = new ArrayList<>();
        counter_w_list = new ArrayList<>();
        terrorist_w_list = new ArrayList<>();

        configFile.load(getClass().getClassLoader().getResourceAsStream("config/weapons_info.cfg"));

        String[] weapons = this.configFile.getStringArray("weapon");
        String[] names = this.configFile.getStringArray("name");
        String[] display_names = this.configFile.getStringArray("display_name");
        String[] recoil_accuracys = this.configFile.getStringArray("recoil_accuracy");
        String[] recoil_recovers = this.configFile.getStringArray("recoil_recover");
        String[] recoil_attacks = this.configFile.getStringArray("recoil_attack");
        String[] firedelays = this.configFile.getStringArray("firedelay");
        String[] prices = this.configFile.getStringArray("price");
        String[] damages = this.configFile.getStringArray("damage");
        String[] ammo_chargers = this.configFile.getStringArray("ammo_charger");
        String[] ammo_totals = this.configFile.getStringArray("ammo_total");
        String[] ranges = this.configFile.getStringArray("range");
        String[] weights = this.configFile.getStringArray("weight");
        String[] primarys = this.configFile.getStringArray("primary");
        String[] types = this.configFile.getStringArray("type");
        String[] teams = this.configFile.getStringArray("team");

        for(int i=0; i<weapons.length; i++) {
            Weapon_info w = new Weapon_info(Integer.parseInt(weapons[i]),names[i],display_names[i],
                    Integer.parseInt(recoil_accuracys[i]),Integer.parseInt(recoil_recovers[i]),Integer.parseInt(recoil_attacks[i]),
                    Integer.parseInt(firedelays[i]),Integer.parseInt(prices[i]),Integer.parseInt(damages[i]),
                    Integer.parseInt(ammo_chargers[i]),Integer.parseInt(ammo_totals[i]),Integer.parseInt(ranges[i]),
                    Double.parseDouble(weights[i]),Boolean.parseBoolean(primarys[i]),Integer.parseInt(types[i]),
                    Integer.parseInt(teams[i]));
            weapon_list.add(w);

            int team = Integer.parseInt(teams[i]);
            if(team == 1) {
                terrorist_w_list.add(w);
            }
            else if(team == 2) {
                counter_w_list.add(w);
            }
            else {
                counter_w_list.add(w);
                terrorist_w_list.add(w);
            }
        }
    }

    /**
     * Return a list of Weapon_info
     * @return weapon_list
     */
    public ArrayList<Weapon_info> getWeapons() {
        return weapon_list;
    }

    public ArrayList<Weapon_info> getTerroristWeapons() { return terrorist_w_list; }

    public ArrayList<Weapon_info> getCounterWeapons() { return counter_w_list; }

}
